<?php

namespace common\components;

use common\tools\qrcode\QRBase;
use common\tools\qrcode\QRstr;
use common\tools\qrcode\QRtools;
use common\tools\qrcode\QRsplit;
use common\tools\qrcode\QRspec;
use common\tools\qrcode\QRrsblock;
use common\tools\qrcode\QRrsItem;
use common\tools\qrcode\QRrs;
use common\tools\qrcode\QRrawcode;
use common\tools\qrcode\QRmask;
use common\tools\qrcode\QRinputItem;
use common\tools\qrcode\QRinput;
use common\tools\qrcode\QRimage;
use common\tools\qrcode\QRencode;
use common\tools\qrcode\QRcode;
use common\tools\qrcode\QRbitstream;
use common\tools\qrcode\FrameFiller;
use common\components\Upload;
use \Imagick;

class QrEMCode
{

    public $value = 'http://www.baidu.com'; //二维码内容 
    public $errorCorrectionLevel = 'L'; //容错级别 
    public $matrixPointSize = 17; //生成图片大小 
    public $logo; //放入二维码的logo图像
    public $logoWidth; //logo宽
    public $saveDir = '../../backend/web/uploads/'; //保存地址
    public $qrCodeName; //保存二维码图片的名称
    public $params;
    public $QrExtension = 'png';
    public $x;
    public $y;
    public $paramString;

    /**
     *
     * @var Imagick
     */
    public $image;

    public function __construct($value, $logo, $saveDir,$params, $QrExtension = 'png')
    {
        //var_dump(is_array($params)) ;exit;
        if (empty($this->image)) {
            $this->image = new Imagick();
        }
        if (!empty($value)) {
            $this->value = $value;
        }
        if (!empty($logo)) {
            $this->logo = $logo;
        }
        if (!empty($saveDir)) {
            $this->saveDir = $saveDir;
        }

        if (!empty($params) && is_array($params)) {
            $this->params = $params;
        }

        //return $this->imageMagic;
    }

    /**
     * 生成二维码图片
     * @return type
     */
    public function makeQrCode()
    {
        $this->makeUrlParams();
        $this->createQrCodeName();
        QRcode::png($this->value, $this->saveDir . $this->qrCodeName, $this->errorCorrectionLevel, $this->matrixPointSize);
        return $this->saveDir . $this->qrCodeName;
    }

    /**
     * 生成 带参数的url
     * @param type $params
     */
    public function makeUrlParams()
    {
        if (is_array($this->params)) {
            foreach ($this->params as $val => $key) {
                if (empty($this->paramString)) {
                    $this->paramString = $key . '=' . $val;
                } else {
                    $this->paramString = $this->paramString . '&' . $key . '=' . $val;
                }
            }
        }
        $this->value = $this->value . '?' . $this->paramString;
    }

    /**
     * 创建二维码图片的名称
     */
    public function createQrCodeName()
    {
        $upload = new Upload([]);
        $this->qrCodeName = microtime(true);
        $shortUrl = $upload->shortUrl($this->qrCodeName);
        $this->qrCodeName = $shortUrl[0] . '.' . $this->QrExtension;
    }

    /**
     * 获取logo图片的宽高
     * @return 
     */
    public function readLogo()
    {
        $this->image->readimage($this->logo);
        //获取图片的宽高
        return $this->image->getimagegeometry();
    }

    /**
     * 获取二维码图片的宽高
     * @return type
     */
    public function readQrCode()
    {

        $this->image->readimage($this->saveDir . $this->qrCodeName);
        return $this->image->getimagegeometry();
    }

    public function groupPicture()
    {
        $this->makeQrCode();
        $logo = $this->readLogo();
        $qr = $this->readQrCode();
        $this->x = intval(($qr['width'] - $logo['width']) / 2);
        $this->y = intval(($qr['height'] - $logo['height']) / 2);
        $this->image->readimage($this->saveDir . $this->qrCodeName);
        //$this->image->readimage($this->logo);
        //$this->image->compositeimage($composite_object, $composite, $x, $y);
        $this->image->compositeImage(new \Imagick($this->logo), Imagick::COMPOSITE_OVER, $this->x, $this->y);

        $this->image->writeimage($this->saveDir . $this->qrCodeName);
    }

}
